package com.java.core.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.java.business.system.permis.bean.User;

/**
 * 
 * @author Session工具类
 * 
 */
public class SessionUtils {

	// 会话用户键
	public static String userContext = "user_session_context";

	/**
	 * 获取当前会话用户
	 * 
	 * @param request
	 * @return
	 */
	public static User getSessionUser(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute(userContext);
		return user;
	}

	/**
	 * 获取当前会话用户
	 * 
	 * @param sessoin
	 * @return
	 */
	public static User getSessionUser(HttpSession sessoin) {
		User user = (User) sessoin.getAttribute(userContext);
		return user;
	}

	/**
	 * 获取当前会话用户的编号
	 * 
	 * @param request
	 * @return
	 */
	public static String getSessionUserId(HttpServletRequest request) {
		User user = getSessionUser(request);
		if (user != null) {
			return user.getUserid();
		}
		return "";
	}

	/**
	 * 设置当前会话用户
	 * 
	 * @param request
	 * @param user
	 */
	public static void setSessionUser(HttpServletRequest request, User user) {
		request.getSession().setAttribute(userContext, user);
	}

	/**
	 * 设置当前会话用户
	 * 
	 * @param session
	 * @param user
	 */
	public static void setSessionUser(HttpSession session, User user) {
		session.setAttribute(userContext, user);
	}

	/**
	 * 移除当前会话用户
	 * 
	 * @param request
	 */
	public static void removeSessionUser(HttpServletRequest request) {
		request.getSession().removeAttribute(userContext);
	}

	/**
	 * 移除当前会话用户
	 * 
	 * @param session
	 */
	public static void removeSessionUser(HttpSession session) {
		session.removeAttribute(userContext);
	}
}
